# -*- encoding: utf-8 -*-
'''
@File    :   common_function.py
@Contact :   ypy02784@163.com
@License :   (C)Copyright 2019-2020, ypy

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2020/3/1 23:27   ypy      1.0         本文件为共用函数单元
'''

from django.apps import apps  # 查询model字段中文名用
from django.core import serializers
from django.core.exceptions import ObjectDoesNotExist

from stock import models


#查询app指定model的数据字段verbose_name
def get_models_verbose_name(appname, modelname):
    """
    查询app指定model的数据字段verbose_name
    :param appname: app名
    :param modelname: model名
    :return: 关于model字段列表的一个verbose_name字典
    """
    modelobj = apps.get_model(appname, modelname)
    fielddic = {}
    for field in modelobj._meta.fields:
        fielddic[field.name] = field.verbose_name
    print(fielddic)
    return fielddic


#查找股票名称列表，并返回，便于web自动填充
def searchinput_autofill(search_txt):
    """
    查找股票名称列表，并返回，便于web自动填充
    :param search_txt: 要查找的股票关键字
    :return: 返回股票名称列表和代码列表
    """
    if (search_txt.isdigit()):
        searchbyname = models.stock_basic.objects.filter(symbol__contains=search_txt)  # 如果输入的是数字，按股票代码查找
    else:
        searchbyname = models.stock_basic.objects.filter(name__contains=search_txt)  # 否则按股票名称查找
    namelist = []
    tscodelist = []
    for name in searchbyname:
        na = name.name
        namelist.append(na)
        tscode = name.ts_code
        tscodelist.append(tscode)
    return namelist, tscodelist


#按股票名称查找股票代码，返回股票代码
def query_ts_code(stockname):
    """
    按股票名称查找股票代码，返回股票代码
    :param stockname:
    :return:
    """
    if (len(stockname) == 9):
        ts_code = stockname
    else:
        try:
            ts_code = models.stock_basic.objects.get(name=stockname).ts_code
            return ts_code
        except ObjectDoesNotExist:
            ts_code = ''
    return (ts_code)


#获取指定股票名称的交易数据
def query_daily_by_stockname(stockname):
    """
    获取指定股票名称的交易数据
    :param stockname:
    :return:
    """
    # 如果输入的是代码
    if (len(stockname) == 9):
        ts_code = stockname
    else:  # 如果输入的是股票名称
        ts_code = query_ts_code(stockname)
    if (ts_code == ''):
        return None
    q_daily = models.daily.objects.filter(ts_code=ts_code).order_by('-trade_date')[0:20]  # 最多返回20条记录
    return serializers.serialize("json", q_daily)


# 获取指定股票名称的交易指标数据
def query_daily_basic_by_stockname(stockname):
    """
    # 获取指定股票名称的交易指标数据
    :param stockname:
    :return:
    """
    # 如果输入的是代码
    if (len(stockname) == 9):
        ts_code = stockname
    else:  # 如果输入的是股票名称
        ts_code = query_ts_code(stockname)
    if (ts_code == ''):
        return None
    q_daily = models.daily_basic.objects.filter(ts_code=ts_code).order_by('-trade_date')[0:20]

    return serializers.serialize("json", q_daily)


 # 获取指定股票名称的大宗交易数据
def query_block_trade_by_stockname(stockname):
    """
    # 获取指定股票名称的大宗交易数据
    :param stockname:
    :return:
    """
    # 如果输入的是代码
    if (len(stockname) == 9):
        ts_code = stockname
    else:  # 如果输入的是股票名称
        ts_code = query_ts_code(stockname)
    if (ts_code == ''):
        return None
    q_daily = models.block_trade.objects.filter(ts_code=ts_code).order_by('-trade_date')[0:20]
    return serializers.serialize("json", q_daily)


# 获取指定股票名称的现金流交易数据
def query_moneyflow_by_stockname(stockname):
    """
    # 获取指定股票名称的现金流交易数据
    :param stockname:
    :return:
    """
    # 如果输入的是代码
    if (len(stockname) == 9):
        ts_code = stockname
    else:  # 如果输入的是股票名称
        ts_code = query_ts_code(stockname)
    if (ts_code == ''):
        return None
    q_daily = models.moneyflow.objects.filter(ts_code=ts_code).order_by('-trade_date')[0:20]
    return serializers.serialize("json", q_daily)


#删除所有股票列表信息
def delete_all_stock_table_infor():
    models.daily.objects.all().delete()
    models.daily_basic.objects.all().delete()
    models.index_daily.objects.all().delete()
    models.stock_basic.objects.all().delete()
    models.stock_company.objects.all().delete()
    models.moneyflow.objects.all().delete()
    models.pre_analysis_main_moneyflow.objects.all().delete()
    models.block_trade.objects.all().delete()
    models.attention_stock.objects.all().delete()

    return "成功删除"